const codeRecord = require("../models/codeRecord")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            if(fields.flag) {
                codeRecord.create({
                    codeRecord_list: JSON.parse(fields.codeRecord_list),
                    user: JSON.parse(fields.user),
                    startTime: fields.startTime,
                    pointId: JSON.parse(fields.pointId),
                    demoId: JSON.parse(fields.demoId),
                    codeDemoId: JSON.parse(fields.codeDemoId)
                }, (error, doc) => {
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({ 'state': true, 'msg': "ok" });
                    }
                })
            } else {
                codeRecord.create({
                    codeRecord_list: fields.codeRecord_list,
                    user: fields.user,
                    startTime: fields.startTime,
                    pointId: fields.pointId,
                    demoId: fields.demoId,
                    codeDemoId: fields.codeDemoId
                }, (error, doc) => {
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({ 'state': true, 'msg': "ok" });
                    }
                })
            } 
        }

    });

});

module.exports = router;